// stores/bookStore.ts
import { defineStore } from 'pinia';

// 定义书籍信息的接口
interface BookInfo {
    title: string;
    author: string;
    price: number; // 假设价格是数字类型
    img: string;
    quantity: number;
}

export const useBookStore = defineStore('bookStore', {
    state: () => ({
        orderedBooks: [] as BookInfo[]
    }),
    getters: {
        orderedBooksCount: (state) => state.orderedBooks.reduce((total, book) => total + book.quantity, 0)
    },
    actions: {
        addOrderedBook(book: BookInfo) {
            const existingBook = this.orderedBooks.find(
                (b) => b.title === book.title && b.author === book.author
            );
            if (existingBook) {
                existingBook.quantity++;
            } else {
                this.orderedBooks.push({ ...book, quantity: 1 });
            }
        }
    }
});